Option Explicit On
Option Strict On

Imports System.Collections
Imports nstl
Imports NUnit.Framework
Imports nstl.Exceptions


Namespace nstl.test
    <TestFixture()> _
 Public Class TestVector

        ''' <summary>
        ''' Initialize() is called once during test execution before
        ''' test methods in this test class are executed.
        ''' </summary>
        <SetUp()> Public Sub Initialize()
            ' TODO: Add test initialization code
        End Sub

        ''' <summary>
        ''' Cleanup() is called once during test execution after
        ''' test methods in this class have executed unless the
        ''' corresponding Initialize() call threw an exception.
        ''' </summary>
        <TearDown()> _
        Public Sub Cleanup()
            ' TODO: Add test cleanup code
        End Sub

        <Test()> _
        Public Sub TestCtors()
            'ctor with a size
            Dim v As New Vector(Of Integer)(3)
            Assert.IsTrue(v(0) = Nothing)
            Assert.IsTrue(v(1) = Nothing)
            Assert.IsTrue(v(2) = Nothing)
            Assert.IsTrue(v.Count() = 3)

            'ctor with a range
            'checks also vector(it, it)!!
            Dim l As New ArrayList
            For i As Integer = 0 To 2 Step 1
                l.Add(i)
            Next
            v = New Vector(Of Integer)(l)
            For i As Integer = 0 To 2 Step 1
                Assert.IsTrue(CType(v(i), Integer) = CType(l(i), Integer))
            Next
        End Sub
        <Test()> _
        Public Sub foreach()
            Dim v As New Vector(Of String)
            v.PushBack("Die")
            v.PushBack("kleine")
            v.PushBack("Raupe")
            v.PushBack("Nimmersatt")

            Dim s As String = String.Empty
            For Each ss As String In v
                s += ss
                s += " "
            Next
            Assert.AreEqual(s, "Die kleine Raupe Nimmersatt ")
        End Sub
        <Test()> _
        Public Sub ReverseIterators()
            Dim va As New Vector(Of Integer)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
            Dim vb As New Vector(Of Integer)

            Algorithm.Copy(va.RBegin, va.REnd, NstlUtil.BackInserter(vb))

            TestUtil.equal(vb, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
        End Sub
        <Test()> _
        Public Sub size()
            Dim va As New Vector(Of Integer)
            va.PushBack(2)
            Assert.IsTrue(va.Count() = 1)
            va.PushBack(2)
            Assert.IsTrue(va.Count() = 2)
            va.PopBack()
            Assert.IsTrue(va.Count() = 1)
        End Sub
        <Test()> _
        Public Sub indexer()
            Dim va As New Vector(Of Integer)
            va.PushBack(2)
            Assert.IsTrue(va(0) = 2)

            'out of range access
            Try
                Dim o As Object = va(2)
                Assert.Fail()
            Catch ex As System.IndexOutOfRangeException
                'good
            Catch ex As System.Exception
                Dim i As Integer = 0
                'Assert.Fail("Unexpected exception was thrown")
            End Try
        End Sub
        <Test()> _
        Public Sub back()
            Dim va As New Vector(Of Integer)
            va.PushBack(1)
            va.PushBack(2)
            Assert.IsTrue(va.Back() = 2)

            va.Clear()
            Try
                Dim i As Integer = va.Back()
                Assert.Fail()
            Catch ex As ContainerEmptyException
                'good :-)
            Catch ex As Exception
                Assert.Fail("An unknown exceptions was thrown!")
            End Try
        End Sub
        <Test()> _
        Public Sub front()
            Dim va As New Vector(Of Integer)
            va.PushBack(1)
            va.PushBack(2)
            Assert.IsTrue(va.Front() = 1)
            va.Clear()
            Try
                Dim i As Integer = va.Front()
                Assert.Fail()
            Catch ex As ContainerEmptyException
                'good :-)
            Catch ex As Exception
                Assert.Fail("An unknown exceptions was thrown!")
            End Try
        End Sub
        <Test()> _
        Public Sub reserve_capacity()
            Dim va As New Vector(Of Integer)
            va.PushBack(1)
            va.PushBack(2)
            Dim size As Integer = va.Capacity() * 2
            va.Reserve(size)
            Assert.IsTrue(va.Count() = 2)
            Assert.IsTrue(va.Capacity() = size)

            'now add untill we reach capacity
            For i As Integer = 0 To size - 1 - 2
                va.PushBack(i)
            Next
            Assert.IsTrue(va.Capacity = va.Count())

            'now let it grow
            va.PushBack(42)
            Assert.IsTrue(va.Capacity = size * 2)
        End Sub
        <Test()> _
        Public Sub resize()
            Dim va As New Vector(Of Integer)
            va.PushBack(2)
            va.Resize(5, Nothing)
            Assert.IsTrue(va.Count() = 5)
            Assert.IsTrue(va(0) = 2)
            Assert.IsTrue(va(1) = Nothing)
            Assert.IsTrue(va(2) = Nothing)
            Assert.IsTrue(va(3) = Nothing)
            Assert.IsTrue(va(4) = Nothing)

            va.Resize(3)
            Assert.IsTrue(va.Count() = 3)
            Assert.IsTrue(va(0) = 2)
            Assert.IsTrue(va(1) = Nothing)
            Assert.IsTrue(va(2) = Nothing)
        End Sub
        <Test()> _
        Public Sub clear()
            Dim va As New Vector(Of Integer)
            For i As Integer = 0 To 9
                va.PushBack(i)
            Next
            Assert.IsTrue(va.Count() = 10)

            va.Clear()
            Assert.IsTrue(va.Count() = 0)
        End Sub
        <Test()> _
        Public Sub empty()
            Dim va As New Vector(Of Integer)
            Assert.IsTrue(va.Empty())
            For i As Integer = 0 To 9
                va.PushBack(i)
            Next
            Assert.IsTrue(va.Count() = 10)

            va.Clear()
            Assert.IsTrue(va.Count() = 0)
            Assert.IsTrue(va.Empty())
        End Sub

        <Test()> _
        Public Sub [erase]()
            Dim va As New Vector(Of Integer)
            For i As Integer = 0 To 9
                va.PushBack(i)
            Next
            Assert.IsTrue(va.Count() = 10)
            '0,1,2,3,4,5,6,7,8,9
            Assert.IsTrue(va.Back() = 9)

            'erase one specific element
            va.Erase(va.End() - 1)
            Assert.IsTrue(va.Back() = 8)
            Assert.IsTrue(va.Count() = 9)

            '0,1,2,3,4,5,6,7,8

            'erase a range
            va.Erase(va.Begin() + 1, va.Begin() + 3)
            '0,3,4,5,6,7,8
            Assert.IsFalse(TestUtil.mismatch(va, New Vector(Of Integer)(0, 3, 4, 5, 6, 7, 8)))
            Assert.IsTrue(va.Count() = 7)
        End Sub
        <Test()> _
        Public Sub insert()
            Dim va As New Vector(Of Integer)
            For i As Integer = 0 To 9
                va.PushBack(i)
            Next
            Assert.IsTrue(va.Count() = 10)
            '0,1,2,3,4,5,6,7,8,9
            Assert.IsTrue(va.Back() = 9)

            'insert one object
            va.Insert(va.Begin() + 1, 42)
            '0,42,1,2,3,4,5,6,7,8,9
            Assert.IsFalse(TestUtil.mismatch(va, New Vector(Of Integer)(0, 42, 1, 2, 3, 4, 5, 6, 7, 8, 9)))

            'insert n objects
            'insert one object
            va.Insert(va.Begin() + 1, 3, 55)
            '0,55,55,55,42,1,2,3,4,5,6,7,8,9
            Assert.IsFalse(TestUtil.mismatch(va, New Vector(Of Integer)(0, 55, 55, 55, 42, 1, 2, 3, 4, 5, 6, 7, 8, 9)))

            'insert range
            Dim vb As New Vector(Of Integer)
            For i As Integer = 0 To 2
                vb.PushBack(i)
            Next
            '0,0,1,2,55,55,55,42,1,2,3,4,5,6,7,8,9
            va.Insert(va.Begin() + 1, vb.Begin(), vb.End())
            Assert.IsFalse(TestUtil.mismatch(va, New Vector(Of Integer)(0, 0, 1, 2, 55, 55, 55, 42, 1, 2, 3, 4, 5, 6, 7, 8, 9)))
        End Sub
        <Test()> _
        Public Sub pop_back()
            Dim va As New Vector(Of Integer)
            For i As Integer = 0 To 9
                va.PushBack(i)
            Next
            Assert.IsTrue(va.Count() = 10)
            '0,1,2,3,4,5,6,7,8,9
            Assert.IsTrue(va.Back() = 9)
            Assert.IsTrue(va.Front() = 0)

            va.PopBack()
            Assert.IsTrue(va.Back() = 8)

            va.Clear()
            Try
                va.PopBack()
                Assert.Fail()
            Catch ex As ContainerEmptyException
                'good :-)
            Catch ex As Exception
                Assert.Fail("An unknown exceptions was thrown!")
            End Try
        End Sub
        <Test()> _
        Public Sub swap()
            Dim va As New Vector(Of Integer)
            For i As Integer = 0 To 9
                va.PushBack(i)
            Next
            Assert.IsFalse(TestUtil.mismatch(va, New Vector(Of Integer)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)))
            Dim vb As New Vector(Of Integer)
            For i As Integer = 0 To 9
                vb.PushBack(2 * i)
            Next
            Assert.IsFalse(TestUtil.mismatch(vb, New Vector(Of Integer)(0, 2, 4, 6, 8, 10, 12, 14, 16, 18)))

            va.Swap(vb)
            Assert.IsFalse(TestUtil.mismatch(va, New Vector(Of Integer)(0, 2, 4, 6, 8, 10, 12, 14, 16, 18)))
            Assert.IsFalse(TestUtil.mismatch(vb, New Vector(Of Integer)(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)))
        End Sub
    End Class
End Namespace


